package main.java.indi.zyj.coderecord;

import main.java.indi.zyj.hot100.base.TreeNode;

import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
import java.util.Stack;

public class PreOrder {


    public List<Integer> preorderTraversal(TreeNode root) {

        List<Integer> result = new ArrayList<>();

        if (root == null) {
            return null;
        }

        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            TreeNode curNode = stack.pop();
            result.add(curNode.val);
            if (curNode.right != null) {
                stack.push(curNode.right);
            }
            if (curNode.left != null) {
                stack.push(curNode.left);
            }
        }

        return result;
    }

}
